<?php


namespace app\user\controller;


use app\common\controller\Api;
use app\user\model\AddressModel;
use app\user\model\DetailModel;
use app\user\model\OrderModel;
use app\user\model\TermModel;
use app\user\validate\OrderValidate;

class Order extends Base
{
    protected $noNeedLogin = '*';

    //查询全部的订单
    public function index()
    {
        $order = OrderModel::order('id','DESC')
            ->field('id,image,goods_name,price,deposit,term_name,num,switch')
            ->select();
        if ($order){
            $this->success('查询成功',[
                'order' => $order
            ]);
        }else{
            $this->error('查询失败');
        }
    }
    //订单详情
    public function order()
    {
        $id = input('id/d');
        if (empty($id)) {
            $this->error('ID不能为空');
        }
        $data = OrderModel::field('id,image,user_name,user_mobile,user_address,goods_name,price,deposit,term_name,num,dp,tol_price,ddbh,wlbh,remarks,switch')
            ->find($id);
        if ($data) {
            $this->success('查询成功',[
                'order' => $data
            ]);
        }
        $this->error('未查询到数据');
    }
    //创建订单
    public function add()
    {
        $data = input('post.');
        $validate = new OrderValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res){
            $this->error($validate->getError());
        }
        $address = AddressModel::find($data['address_id']);
        if (!$address){
            $this->error('地址ID不正确');
        }
        $goods = DetailModel::find($data['goods_id']);
        if (!$goods){
            $this->error('商品ID不正确');
        }
        $term = TermModel::find($data['term_id']);
        if(!$term){
            $this->error('期赁ID不正确');
        }
        //到期时间
        if($term['term'] == '一周'){
            $exp_time = date('Y-m-d H:i:s',strtotime("+1 week"));
        } elseif ($term['term'] == '两周'){
            $exp_time = date('Y-m-d H:i:s',strtotime("+2 week"));
        } elseif ($term['term'] == '一个月'){
            $exp_time = date('Y-m-d H:i:s',strtotime("+1 month"));
        }
        //总价
        $tol = round($goods['money'] * $data['num'] * $data['num'],2);
        //封面
        $image =  '/' . ltrim($goods['image'],request()->domain());
        $data2 = [
            'user_id' => $data['user_id'],
            'image' => $image,
            'user_name' => $address['name'],
            'user_mobile' => $address['mobile'],
            'user_address' => $address['area'] . $address['address'],
            'goods_name' => $goods['name'],
            'goods_id' => $goods['id'],
            'price' => $goods['money'],
            'deposit' => $goods['deposit'],
            'num' => $data['num'],
            'term_id' => $term['id'],
            'term_name' => $term['term'],
            'dp' => $data['dp'],
            'tol_price' => $tol,
            'pay_type' => $data['pay_type'],
            'remarks' => $data['remarks'],
            'ddbh' => date('YmdHis').mt_rand(1111,9999).createRandStr(4),
            'wlbh' => mt_rand(1111,9999).createRandStr(4),
            'create_time' =>date('Y-m-d H:i:s'),
            'exp_time' => $exp_time
        ];
        $add = OrderModel::create($data2);
        if ($add) {
            $this->success('提交成功');
        }else{
            $this->error('提交失败');
        }
    }

}